package 三数之和;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Solution {

    public static void main(String[] args) {
        int[] nums = {-1,0,1,2,-1,-4};
//        int[] nums = {0,0,0,0};
//        int[] nums = {1,2,-2,-1};
//        int[] nums = {-2,0,1,1,2};
        System.out.println(threeSum(nums));
    }

    public static List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));

        List list = new ArrayList();
        if (nums.length < 3){
            return list;
        }

        for (int i = 0; i < nums.length; ++i) {
            int l = i+1;
            int r = nums.length-1;
            int target;
            System.out.println("循环 "+nums[i]);
            while (l < r){
                List item = new ArrayList();
                target = nums[i]+nums[l]+nums[r];
                System.out.println( nums[i]+"+"+nums[l]+"+"+nums[r]);
                if (target > 0){
                    --r;
                }
                if (target < 0){
                    ++l;
                }
                if (target == 0){
                    System.out.println("i:"+i+ " l:"+l+" r:"+r );
                    item.add(nums[i]);
                    item.add(nums[l]);
                    item.add(nums[r]);
                    list.add(item);
                    ++l;
                    --r;
                }
            }
        }
        List collect = (List) list.stream().distinct().collect(Collectors.toList());
        return collect;
    }
}
